const mongoose = require("mongoose");
const autoIncrementFactory = require("mongoose-sequence");
const autoIncreament = autoIncrementFactory(mongoose);
const userSchema = new mongoose.Schema(
  {
    username: {
      type: String,
      unique: true,
      required: true,
    },
    password: {
      type: String,
    },
    email: {
      type: String,
    },
    nickname: {
      type: String,
    },
    picure: {
      type: String,
    },
    phoneNumber: {
      type: String,
      //TODO 在其它登录方式中为null, 如果设置了unique, 则不能为null，因为unique要求字段值唯一
      // unique: true,
    },
    type: {
      type: String,
      enum: ["email", "cellphone", "oauth"],
      default: "email",
    },
    provider: {
      type: String,
      enum: ["gitee"],
      default: "gitee",
    },
    oauthId: {
      type: String,
    },
    role: {
      type: String,
      enum: ["admin", "normal", "moderator"],
      default: "normal",
    },
  },
  {
    // 添加createdAt 和 updatedAt 字段，并且自动更新
    timestamps: true,
    toJSON: {
      transform(_doc, ret) {
        delete ret.password;
        delete ret.__v;
      },
    },
  }
);
userSchema.plugin(autoIncreament, { inc_field: "id" });
module.exports = userSchema;
